package com.smartcruise.start.checkVerifyCode;

import android.text.Editable;
import android.text.TextWatcher;
import android.widget.Button;
import android.widget.TextView;

import com.smartcruise.R;
import com.smartcruise.base.BaseTitleFragment;
import com.smartcruise.base.TitleBuilder;
import com.smartcruise.start.modifyPassword.ModifyPasswordFragment;
import com.smartcruise.util.FragmentUtils;
import com.smartcruise.util.StringUtils;
import com.smartcruise.util.customView.ClearableEditTextWithIcon;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by FengChaoQun
 * on 2018/3/17 0017
 * 找回密码 输入手机号和验证码界面
 */

public class VerifyFragment extends BaseTitleFragment implements VerifyContract.View {
    @BindView(R.id.phoneNumber)
    ClearableEditTextWithIcon phoneNumber;
    @BindView(R.id.sendVerifyCode)
    TextView sendVerifyCode;
    @BindView(R.id.code)
    ClearableEditTextWithIcon code;
    @BindView(R.id.submit)
    Button submit;

    private VerifyContract.Presenter mPresenter;

    @Override
    protected void init() {
        mPresenter = new VerifyPresenterImpl(this);
        mPresenter.start();

        TextWatcher textWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                setButtonEnable(StringUtils.isNotEmpty(phoneNumber, code) && phoneNumber.length() == 11 && code.length() >= 4);
                setSendCodeEnable(phoneNumber.length() == 11);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        };
        phoneNumber.addTextChangedListener(textWatcher);
        code.addTextChangedListener(textWatcher);
    }

    @Override
    protected int getContentLayout() {
        return R.layout.fragment_content_verify;
    }

    @Override
    protected TitleBuilder getTitleBuilder() {
        return TitleBuilder.create(true, getString(R.string.findPassword));
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mPresenter.destroy();
    }

    @Override
    public void onLeftArrowClicked() {
        getActivity().finish();
    }

    @Override
    public String getPhoneNumber() {
        return phoneNumber.getText().toString();
    }

    @Override
    public String getCode() {
        return code.getText().toString();
    }

    @Override
    public void setButtonEnable(boolean enable) {
        submit.setAlpha(enable ? 1.f : 0.5f);
    }

    @Override
    public void setSendCodeEnable(boolean enable) {
        sendVerifyCode.setAlpha(enable ? 1.f : 0.5f);
    }

    @Override
    public void setSendCodeText(String text) {
        sendVerifyCode.setText(text);
    }

    @Override
    public void gotoSetPassword() {
        FragmentUtils.replace(getFragmentManager(), R.id.container, ModifyPasswordFragment.getInstance(true, getPhoneNumber()));
    }

    @OnClick(R.id.sendVerifyCode)
    public void onSendVerifyCodeClicked() {
        if (phoneNumber.length() == 11) {
            mPresenter.clickOnSendCode();
        } else {
            showToast("请输入11位手机号码");
        }
    }

    @OnClick(R.id.submit)
    public void onSubmitClicked() {
        if (StringUtils.isNotEmpty(phoneNumber, code) && phoneNumber.length() == 11 && code.length() >= 4) {
            mPresenter.clickOnSubmit();
        } else {
            if (!StringUtils.isNotEmpty(phoneNumber, code)) {
                showToast("手机号码和验证码不能为空");
            } else if (phoneNumber.length() != 11) {
                showToast("请输入完整的11位手机号码");
            } else {
                showToast("请输入不少于4位的验证码");
            }
        }

    }
}
